<?php

	/**
	 * @see Zend_Controller_Action
	 */
	require_once 'Zend/Controller/Action.php';
	
	
	class Bike_Controller_Action extends Zend_Controller_Action
	{
	    protected $_reflection;
	    protected $_auth;

        protected function getReflection()
        {
        	if(is_null($this->_reflection))
        	{
        		$this->_reflection = new Zend_Reflection_Class($this);
        	}
        	return $this->_reflection;
        }

        public function getConfig()
	    {
			echo "getConfig() method here!";
	    }

	    public function getDbProfiler()
	    {
	    	return $this->getBootstrap()
	    				->getPluginResource('multidb')
	    				->getDefaultDb()
	    				->getProfiler();
	    }

	    public function getBootstrap()
	    {
	    	return $this->getFrontController()->getBootstrap();
	    }

	    public function getCache($Code)
	    {
			return $this->getBootstrap()
			                ->getPluginResource('cachemanager')
			                ->getCacheManager()
	    					->getCache($Code);
	    }

	    public function translate()
	    {
			$args = func_get_args();
			$Translate = $this->getBootstrap()->getResource('translate');
			return call_user_func_array(array($Translate, 'translate'), $args);
	    }

	    public function getDefaultModule()
	    {	    	return $this->getFrontController()->getDefaultModule();
	    }
	    
	    public function getRenderer()
	    {
	    	return $this->isAdminArea() ? Bike_Form_Renderer_ExtJs::TYPE : Bike_Form_Renderer_Standard::TYPE;
	    }
	    
	    public function getLayoutName()
	    {
	    	if($this->isAdminArea())
	    	{
	    		return 'ExtJs';
	    	}
	    	return 'Main';
	    }

	    public function init()
	    {
	    	parent::init();
	    	
	    	$Area = $this->getArea()->getArea();
	    	
	    	$this->_helper->layout->setInflectorTarget(':area/:script.:suffix')
	    						  ->setLayout($this->getLayoutName())
	    						  ->getInflector()->setStaticRule('area', $Area);

	    	$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
	    	$viewRenderer->setViewScriptPathSpec(':controller/:area/:action.:suffix')
	    				 ->setViewScriptPathNoControllerSpec(':area/:action.:suffix')
	    				 ->getInflector()->setStaticRule('area', $Area);
	    	
	    	$contextSwitch = $this->_helper->getHelper('ContextSwitch');
	    	$contextSwitch->setAutoJsonSerialization(False);
	    	$contextSwitch->initContext();

	    	$ajaxContext = $this->_helper->getHelper('AjaxContext');
        	$ajaxContext->setAutoJsonSerialization(False);
        	$ajaxContext->initContext();
	    }

	    public function preDispatch()
	    {
            if($this->isAdminArea() && $this->getRequest()->getModuleName() != $this->getDefaultModule())
	    	{	    		$this->_forward('login', 'auth', $this->getDefaultModule());
	    	}

	    }

	    public function __call($methodName, $args)
	    {
	        if('action' == substr($methodName, 0, 6))
	        {
	            $action = substr($methodName, 6);
	            throw new Zend_Controller_Action_Exception(sprintf('Action "%s" does not exist and was not trapped in __call()', $action), 404);
	        }

	        return parent::__call($methodName, $args);
	    }

	    public function getView()
	    {
	    	return $this->view;
	    }

	    protected function getAuth()
	    {
	    	if(is_null($this->_auth))
	    	{
	    		$this->_auth = Zend_Auth::getInstance();
	    		$this->_auth->setStorage(new Zend_Auth_Storage_Session($this->getArea()->getArea()));
	   		}
	   		return $this->_auth;
	    }

	    public function getArea()
	    {
	    	return Bike_Area::getInstance();
	    }

	    public function isArea($Area)
	    {
	    	return (bool) ($this->getArea()->isUserArea($Area));
	    }

	    public function isAdminArea()
	    {
	    	return $this->getArea()->isAdminArea();
	    }

	    public function isFrontArea()
	    {
	    	return $this->getArea()->isFrontArea();
	    }

	}

